/**
 * @author harae Feng 
 * @date 2019-10-20 14:57:19 
 * @Last Modified by: harae Feng
 * @Last Modified time: 2019-10-20 16:24:03
 * @Describe 铃声多多-来电铃声-视频
 */


const fetch = require('../../util/fetchUtil').fetchRequest
const ResultAPI = require('../../routes/api/resultAPI')
const CatchHandle = require('../../routes/api/apicatch')
const cryptoDES = require('./__crypto')
const moment = require('moment')
const querystring = require('querystring')
const xml2js = require('xml2js')

/**
 * 来电铃声
 */
async function videoCallShowList(options){
    const __parms = {
        user: `868840046120838`,
        prod: `RingDD_ar_8.7.70.0`,
        isrc: `RingDD_ar_8.7.70.0_vivo.apk`,
        mac: `FCBE7B1FB16D`,
        dev: `vivo>PD1809>V1809A`,
        vc: `60087700`,
        loc: `CN`,
        sp: `cm`,
        uid: ``,
        unionid: ``,
        tuid:`call_show_list`, // 必须
        type: `getcallshowlist`, // 必须
        page: options.page ? options.page : 0, // 必须
        pagesize: options.pagesize ? options.pagesize : 25 , // 必须
        sig: ``,
        ptime: moment(new Date()).format('YYYY-MM-DD'),
        tstamp: Date.parse(new Date()) / 1000
    }
    const qString = querystring.stringify(__parms)
    const q = cryptoDES.encrypt(qString)

    let url = `http://ring.shoujiduoduo.com/ring_enc.php?cmd=getcallshowlist&q=${q}`
    let res = await fetch(url, {
        headers: {
            'Host': 'ring.djduoduo.com',
			'User-Agent': 'Apache-HttpClient/UNAVAILABLE (java 1.4)',
        }
    })
    let resXML = await res.text()
    let resJSON = await xml2js.parseStringPromise(resXML)
    return resJSON
}

async function videoCallShowListAPI(ctx){
    try {
        let parms = ctx.query
        if(!parms.page && !parms.pagesize){
            return new ResultAPI(10000)
        }
        let resJSON = await videoCallShowList(parms)
        if(resJSON.page && resJSON.page.video.length){
            return new ResultAPI(0, {}, resJSON)
        }else{
            return new ResultAPI(10003)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
}

module.exports = {
    videoCallShowListAPI: videoCallShowListAPI
}